home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 10-InfoSystems / netscape_info / security-i.frm.z / security-i.frm
Encoding:
Text File  |  1997-07-30  |  10.4 KB  |  426 lines

  1. #!/usr/bin/perl5
  2. #
  3. # security-i.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: security-i.frm,v 1.5 1997/06/19 22:26:11 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $pid_file = "/usr/ns-home/admserv/pid";
  26. $start_ns_admin = "/usr/ns-home/start-admin > /dev/null 2>&1";
  27. $stop_ns_admin = "/usr/ns-home/stop-admin > /dev/null 2>&1";
  28.  
  29. $title = "Netscape Admin Server Access Control";
  30. $myname = "security-i.cgi";
  31. $conf_file = "/usr/ns-home/admserv/ns-admin.conf";
  32. $pw_file = "/usr/ns-home/admserv/admpw";
  33. $dummy = "/usr/ns-home/admserv/ns-admin.conf.tmp";
  34.  
  35. $account_type_lc = "account";
  36. $account_type_uc = "Account";
  37.  
  38. $js_generic = 
  39. "$js_error_box
  40. $js_ip
  41. $js_hostname
  42. $js_meta
  43. which = \"none\";
  44. function runSubmit() {
  45.     if(which == \"add\") return runAdd();
  46.     if(which == \"edit\") return runEdit();
  47.     if(which == \"delete\") return runDelete();
  48.     return runDo();
  49. }
  50. function markAdd() { which = \"add\"; }
  51. function markEdit() { which = \"edit\"; }
  52. function markDelete() { which = \"delete\"; }
  53. function is_ip(word) {
  54.     ipChars = \"0123456789\.*\";
  55.     for (ii=0; ii<word.length; ii++) {
  56.         c = word.charAt(ii);
  57.                        if (ipChars.indexOf(c, 0) == -1) {
  58.             return (false);
  59.         }
  60.     }
  61.     return (true);
  62. }
  63. function remove_star(word) {
  64.     if (word.indexOf(\"*\", 0) == -1) return (word);
  65.     new_word = \"\";
  66.     for (jj = 0; jj < word.length; jj++) {
  67.         if (word.charAt(jj) == \"*\") 
  68.             new_word = new_word + \"233\";
  69.         else 
  70.             new_word = new_word + word.charAt(jj);
  71.     }
  72.     return (new_word);
  73. }             
  74. function testStarList(Ctrl) {
  75.     my_count = 0;
  76.     ipChars = \"0123456789\.*\";
  77.     whitespace = \" \\n\\r\\t\\f\";  space = true;
  78.     for(start=0, cur=0; cur < Ctrl.value.length; cur++) {
  79.         for(i = 0; i < whitespace.length; i++) {
  80.             c = whitespace.charAt(i);
  81.             if (Ctrl.value.charAt(cur) == c) { // found whitespace
  82.                 if (space == false) { 
  83.                     space = true;  
  84.                     if(cur != start) { 
  85.                         host = Ctrl.value.substring(start,cur); 
  86.                         my_count++; 
  87.                         if (is_ip(host)) {
  88.                             new_host = remove_star(host);
  89.                             if (!testIPaddress(new_host,false)) { 
  90.                                 errorBox (Ctrl, \"Invalid server IP address: \" + host + \"\\nin server address list.\"); 
  91.                                 return (false); 
  92.                             }
  93.                         } else {
  94.                             if (!testHostname(Ctrl, host, \"hostname\", 0)) return (false);
  95.                         } 
  96.                     } 
  97.                 }
  98.                 break;
  99.             } else {                           // found character
  100.                 if (space == true) { space = false;  start = cur; }
  101.             }
  102.         }
  103.     }
  104.     if(cur != start && space == false) { 
  105.         host = Ctrl.value.substring(start,cur); 
  106.         my_count++; 
  107.         if (is_ip(host)) {
  108.             new_host = remove_star(host);
  109.             if (!testIPaddress(new_host,false)) { 
  110.                 errorBox (Ctrl, \"Invalid server IP address: \" + host + \"\\nin server address list.\"); 
  111.                 return (false); 
  112.             } 
  113.         } else {
  114.             if (!testHostname(Ctrl, host, \"hostname\", 0)) return (false);
  115.         }
  116.     }
  117.     return (true);
  118. }
  119. function runDo() {
  120.     if (!testStarList(document.AccountForm.clients)) return (false);
  121.     return (true);
  122. }        
  123. function runAdd() {
  124.     Ctrl = document.AccountForm.new_account;
  125.     if (Ctrl.value.length == 0) {
  126.         errorBox(Ctrl, \"To add a new $account_type_lc, first enter the new $account_type_lc name.\");
  127.         return (false);
  128.     }
  129.     if (!testMeta(Ctrl, \"account name\")) return (false);
  130.     return (true);
  131. }
  132. function runEdit() {
  133.     Ctrl = document.AccountForm.chosen;
  134.     none = true;
  135.     for(i = 0; i < Ctrl.length; i++) { 
  136.         if (Ctrl.options[i].selected) {
  137.             none = false; 
  138.             break;
  139.         }
  140.     }
  141.     if (none) { 
  142.         errorBox (Ctrl, \"To edit an existing $account_type_lc, first select \"
  143.             + \"one from\\nthe $account_type_lc, then click the Edit button.\");
  144.         return (false);
  145.     }
  146.     return (true);
  147. }
  148. function runDelete() {
  149.     Ctrl = document.AccountForm.chosen;
  150.     none = true;
  151.     for(i = 0; i < Ctrl.length; i++) { 
  152.         if (Ctrl.options[i].selected) {
  153.             none = false; 
  154.             break;
  155.         }
  156.     }
  157.     if (none) { 
  158.         errorBox (Ctrl, \"To delete an existing $account_type_lc, first select \"
  159.             + \"one from\\nthe $account_type_lc, then click the Delete button.\");
  160.         return (false);
  161.     }
  162.     return (true);
  163. }";
  164.  
  165. $js_edit = 
  166. "$js_standard
  167. $js_error_box
  168. $js_meta
  169. function checkForm(form)  {
  170.     if (form.uname.value.length == 0) {
  171.         errorBox(form.uname, \"Account name cannot be empty.\");
  172.         return (false);
  173.     }
  174.     if (form.password.value.length == 0) {
  175.         errorBox(form.uname, \"Password cannot be empty.\");
  176.         return (false);
  177.     }
  178.     if (!testMeta(form.uname, \"account name\")) return (false);
  179.     if (!testMeta(form.password, \"password\")) return (false);
  180.     return (true);
  181. }";
  182.  
  183. print "Content-type: text/html\n\n";
  184.  
  185. &get_fields;
  186. &get_accounts;
  187.  
  188. if (%fld) {
  189.     $fld{'chosen'} =~ /([\w.-]+)/; $chosen = $1;
  190.  
  191.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  192.     $help =~ s/cgi$/hlp/;
  193.     exec $help if ($fld{'help'} eq "Help");
  194.  
  195.     if ($fld{'edit'}) {
  196.         &get_edit($chosen);
  197.     } elsif ($fld{'do_edit'}) {
  198.         &generic;
  199.     } elsif ($fld{'add'}) {
  200.         &get_add;
  201.     } elsif ($fld{'delete'}) {
  202.         &generic($chosen);    
  203.     } elsif ($fld{'doit'}) {
  204.         &do_it;
  205.         &get_accounts;
  206.         &generic;
  207.     } elsif ($fld{'doedit'}) {
  208.         &do_edit;
  209.         &get_accounts;
  210.         &generic;
  211.     }
  212. } else { &generic; }
  213.  
  214. sub do_it {
  215.     &do_delete($fld{'deleted'}) if $fld{'deleted'};
  216.     &process_clients;
  217.     &bounce_server;
  218. }
  219.  
  220. sub process_clients {
  221.     @clients = split(/\s+/, $fld{'clients'});
  222.     foreach (@clients) {
  223.         if ($_ =~ /[^0-9\.\*]/) { push(@hosts, $_); }
  224.         else { push(@adds, $_); }
  225.     }
  226.     if ($#hosts > 0) { $hosts = "(" . join('|', @hosts) . ")"; }
  227.     elsif ($#hosts == 0) { $hosts = $hosts[0]; }
  228.     else { $hosts = ""; }
  229.  
  230.     if ($#adds > 0) { $adds = "(" . join('|', @adds) . ")"; }
  231.     elsif ($#adds == 0) { $adds = $adds[0]; }
  232.     else { $adds = ""; }
  233.  
  234.     $hosts_added = 0;
  235.     $adds_added = 0;
  236.     open(IN, "< $conf_file");
  237.     open(OUT, "> $dummy");
  238.     while(<IN>) {
  239.         if ($_ =~ /^Hosts\s/) {
  240.             $hosts_added = 1;
  241.             if ($hosts ne "") { print OUT "Hosts $hosts\n"; }
  242.         } elsif ($_ =~ /^Addresses\s/) {
  243.             $adds_added = 1;
  244.             if ($adds ne "") { print OUT "Addresses $adds\n"; }
  245.         } else { print OUT $_; }
  246.     }
  247.     if ($hosts ne "" && $hosts_added == 0) {
  248.         print OUT "Hosts $hosts\n"; 
  249.     }
  250.     if ($adds ne "" && $adds_added == 0) {
  251.         print OUT "Addresses $adds\n";
  252.     }
  253.  
  254.     close(IN);
  255.     close(OUT);
  256.  
  257.     rename($dummy, $conf_file);
  258.  
  259.     $message .= "  Client list updated.";
  260. }
  261.  
  262. sub bounce_server {
  263.     open(IN, "< $pid_file");
  264.     $pid = <IN>;
  265.     close(IN);
  266.     if ($pid) {
  267.         system($stop_ns_admin);
  268.         system($start_ns_admin);
  269.     }
  270. }
  271.  
  272. sub do_delete {
  273.     open(IN, "< $pw_file");
  274.     open(OUT, "> $dummy");
  275.     while(<IN>) {
  276.         print OUT $_ unless $_[0] eq (split(/:/, $_))[0];
  277.     }
  278.     close(IN);
  279.     close(OUT);
  280.     
  281.     rename($dummy, $pw_file);
  282.     &bounce_server;
  283.  
  284.     $message .= "$account_type_uc deleted.";
  285. }
  286.  
  287. sub do_edit {
  288.     $salt = &mksalt;
  289.     $pswd = crypt($fld{'password'}, $salt);
  290.     $line = $fld{'uname'} . ":" . $pswd;
  291.  
  292.     if ($fld{'old_account'} ne "") {
  293.         open(IN, "< $pw_file");
  294.         open(OUT, "> $dummy");
  295.         while(<IN>) {
  296.             if ($fld{'old_account'} eq (split(/:/, $_))[0]) {
  297.                 print OUT "$line\n";
  298.             } else { print OUT $_; }
  299.         }
  300.         close(IN);
  301.         close(OUT);
  302.  
  303.         rename($dummy, $pw_file);
  304.  
  305.         $message .= "$account_type_uc edited.";
  306.     } else { 
  307.         open(OUT, ">> $pw_file");
  308.         print OUT "$line\n";
  309.         close(OUT);
  310.  
  311.         $message .= "$account_type_uc added.";
  312.     }
  313.     
  314.     &bounce_server;
  315. }
  316.  
  317. sub get_add {
  318.     &get_edit($fld{'new_account'});
  319. }
  320.  
  321. sub get_edit {
  322.     &js_title_block($title, $js_edit);
  323.     if ($fld{'add'}) { &header_block("Add $account_type_uc"); }
  324.     else { &header_block("Edit $account_type_uc"); }
  325.  
  326.     print "<form name=StandardForm action=$myname method=post "
  327.         . "onSubmit=\"return runSubmit()\">\n"
  328.         . "<i>$message</i>\n"
  329.         . "<center><br><table width=400>\n";
  330.  
  331.     if ($fld{'edit'}) {
  332.         print "<input type=hidden name='old_account' value=$_[0]>\n";
  333.     }
  334.  
  335.     print "<tr><th align=left>$account_type_uc name:<td>"
  336.         . &text("uname", $_[0], 20)
  337.         . "</td></tr>\n";
  338.  
  339.     print "<tr><th align=left>$account_type_uc password:<td>"
  340.         . &text("password", $val{'password'}, 20)
  341.         . "</td></tr>\n";
  342.  
  343.     print "</table></center><br>\n";
  344.  
  345.     print &js_buttons('doedit','Ok','onClick="markOK()"','onClick="markOther()"');
  346. }
  347.     
  348. sub get_accounts {
  349.     undef @accounts;
  350.     open(IN, "< $pw_file");
  351.     while(<IN>) {
  352.         push(@accounts, (split(/:/, $_))[0]);
  353.     }
  354.     close(IN);
  355.  
  356.     undef @clients;
  357.     open(IN, "< $conf_file");
  358.     while(<IN>) {
  359.         if ($_ =~ /^Hosts\s+(.*)/) {
  360.             $list = $1;
  361.             if ($list =~ /\((.*)\)/) {
  362.                 push(@clients, split(/\|/, $1));
  363.             } else {
  364.                 push(@clients, $list);
  365.             }
  366.         } elsif ($_ =~ /^Addresses\s+(.*)/) {
  367.             $list = $1;
  368.             if ($list =~ /\((.*)\)/) {
  369.                 push(@clients, split(/\|/, $1));
  370.             } else {
  371.                 push(@clients, $list);
  372.             }
  373.         }
  374.     }
  375.     close(IN);
  376.     $val{'clients'} = join("\n", @clients);
  377. }
  378.  
  379. sub generic {
  380.     &js_title_block($title, $js_generic);
  381.     &header_block($title);
  382.     
  383.     if ($_[0]) { $message .=  qq|Click "Ok" to save changes.|; }
  384.  
  385.     print "<form name=AccountForm action=$myname method=post "
  386.         . "onSubmit=\"return runSubmit()\">\n";
  387.  
  388.     print "<i>$message</i>\n"
  389.         . "<br>\n"
  390.         . "<h3>Allowed users:</h3><center><table width=400>\n"
  391.         . "<tr><td><input type=submit name=\"add\" onClick=\"markAdd()\" \n"
  392.         . "value=\"Add New $account_type_uc\"\n"
  393.         . "</td><td><input name=\"new_account\" value=\"$val{'new_account'}\"\n" 
  394.         . "size=19 onClick=\"markAdd()\"></td></tr>\n";
  395.         
  396.     if ($#accounts >= 0) {
  397.         if ($_[0]) {
  398.             undef @show_accts;
  399.             foreach $arg (@accounts) {
  400.                 push(@show_accts, $arg) if $arg ne $_[0];
  401.             }
  402.             print "<input type=hidden name=deleted value=$_[0]>\n";
  403.         } else { @show_accts = @accounts; }
  404.  
  405.         print "<tr><td>\n"
  406.             . "<input type=submit name=\"edit\" onClick=\"markEdit()\" \n"
  407.             . "value=\"Edit Selected $account_type_uc\"></td><td rowspan=2>";
  408.         print &choice_list(*show_accts, "chosen", 20);
  409.         print "</td/tr>\n"
  410.             . "<tr><td><input type=submit name=\"delete\" onClick=\"markDelete()\" \n"
  411.             . "value=\"Delete Selected $account_type_uc\"></td></tr>\n";
  412.     }
  413.  
  414.     print "</table></center><br>";
  415.  
  416.     print "<h3>Allowed clients:</h3>\n"
  417.         . "<center>\n"
  418.         . "<textarea name='clients' cols=20 rows=4>"
  419.         . $val{'clients'}
  420.         . "</textarea><br><br>\n";
  421.  
  422.     print &buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"');
  423. }
  424.  
  425.         
  426.